package com.xinchen.aiowaste.ui.activity

import android.os.Bundle
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.activity.viewModels
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.maple.model.data.MainMenuData
import com.maple.utils.const.ConstKey
import com.maple.utils.ext.toGone
import com.maple.utils.ext.toVisible
import com.maple.utils.store.AioDBHelper
import com.maple.utils.util.LogUtils
import com.maple.utils.widget.timer.MyTimer
import com.maple.utils.widget.timer.MyTimerTask
import com.xinchen.aiowaste.R
import com.xinchen.aiowaste.base.BaseActivity
import com.xinchen.aiowaste.ui.adapter.MainMenuAdapter
import com.xinchen.aiowaste.vm.HomeViewModel

class HomeActivity : BaseActivity() {

    private var rvList: RecyclerView? = null

    private var llTitleLeft: LinearLayout? = null
    private var ivLogoName: ImageView? = null

    private val viewModel by viewModels<HomeViewModel>()

    private val listAdapter: MainMenuAdapter by lazy {
        MainMenuAdapter(this).apply {
            this.setData(listOf(
                MainMenuData("入库管理",R.drawable.ic_menu_in,"In Storage"),
                MainMenuData("出库管理",R.drawable.ic_menu_out,"Out Storage"),
                MainMenuData("标签查询",R.drawable.ic_menu_label,"Label The Query"),
                MainMenuData("设备管理",R.drawable.ic_menu_device,"About Equipment"),
                MainMenuData("仓库数据",R.drawable.ic_menu_data,"Warehouse Data"),
                MainMenuData("系统设置",R.drawable.ic_menu_setting,"System Setting"),
                MainMenuData("标签打印",R.drawable.ic_menu_label,"Label Print"),
                MainMenuData("测试",R.drawable.ic_menu_label,"Label Print"),
            ))
            this.setListener(object :MainMenuAdapter.OnClickListener{
                override fun onItemClick(pos: Int, item: MainMenuData?) {
                    item?.let {
                        showToast(it.name)
                        when(it.name) {
                            "入库管理" -> {
                                onStartActivity(InStorageActivity::class.java)
                            }
                            "出库管理" -> {
                                onStartActivity(OutStorageActivity::class.java)
                            }
                            "标签查询" -> {

                            }
                            "设备管理" -> {

                            }
                            "仓库数据" -> {

                            }
                            "系统设置" -> {
                                onStartActivity(SystemSettingActivity::class.java)
                            }
                            "标签打印" -> {
                                onStartActivity(LabelListActivity::class.java)
                            }
                            "测试" -> {
                                val bundle: Bundle = Bundle()
                                bundle.putString(ConstKey.BundleKey.EXTRA_TYPE,WasteListActivity.TYPE_IN_DANGEROUS)
                                bundle.putString(ConstKey.BundleKey.EXTRA_CODE,"GF191F1B1A191D1B1E1E1C19131F1A1B1B1B1B1B1A2B")
                                onStartActivity(PrintLabelActivity::class.java,bundle = bundle)
                            }
                            else -> LogUtils.logGGQ("not known")
                        }
                    }
                }
            })
        }
    }

    override fun getLayoutId(): Int = R.layout.activity_home

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        llTitleLeft = findViewById(R.id.ll_title_left)
        llTitleLeft?.toGone()
        ivLogoName = findViewById(R.id.iv_logo_name)
        ivLogoName?.toVisible()
        showClockTime()
        rvList = findViewById(R.id.rv_list)
        rvList?.let {
            it.layoutManager = GridLayoutManager(this, 2)
            it.adapter = listAdapter
        }
    }
    override fun initData(savedInstanceState: Bundle?) {
        viewModel.defUI.showDialog.observe(this, Observer {
            showLoading()
        })

        viewModel.defUI.dismissDialog.observe(this, Observer {
            dismissLoading()
        })

        viewModel.defUI.toastEvent.observe(this, Observer {
            showToast(it)
        })

        viewModel.devData.observe(this, Observer{
            AioDBHelper.saveScalesNo(it.scalesNo)
            this.lifecycle.addObserver(MyTimer(object :MyTimerTask{
                override fun onTask() {
                    LogUtils.logGGQ("task")
                    viewModel.getEnvInfo(it.scalesNo)
                }
            }, period = 3000))
        })

        viewModel.getDevInfo()

    }
}